package com.dt76.smallloanbackenduser.controller.hx;

import com.dt76.smallloanbackenduser.pojo.huxuan.Vip;
import com.dt76.smallloanbackenduser.remote.huxuan.SetMatonRemote;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/hxvip")
public class VipController {

    @Autowired
    private SetMatonRemote setMatonRemote;
    //查询所有会员信息
    @GetMapping("/all")
    public String qryAll(Model model){
        System.out.println("进入！");
        List<Vip> vips = setMatonRemote.qryAll();
        for(Vip vip:vips){
            System.out.println("打印！！！！！！！！！！！");
            System.out.println(vip.toString());

        }
        model.addAttribute("vips",vips);
        return "/pages/hx/viprank";
    }
    //修改会员等级
    @GetMapping("/updrank")
    @ResponseBody
    public Map<String,Object> updRankAll(int id,int rank){
        Map<String,Object> resultMap = new HashMap<String, Object>();
        if(rank == 1){
            //修改全字段
            int ranknum = setMatonRemote.updRankAll();
            System.out.println("修改条数"+ranknum);
            //修改单个
            Vip vip = new Vip();
            vip.setId(id);
            vip.setDefault_level(rank);
            int updRank = setMatonRemote.updRank(vip);
            System.out.println("单个修改"+updRank+">>>id"+id+">>rank"+rank);
        }else{
            //修改全字段
            int ranknum = setMatonRemote.updRankAll();
            System.out.println("修改条数"+ranknum);
        }
        resultMap.put("result","修改成功！");
        return resultMap;
    }
    //修改会员信息
    @GetMapping("/updvipinfo")
    public String vipInfo(){
        return "/pages/hx/viprank-add";
    }
    //删除会员
    @GetMapping("/del/{id}")
    @ResponseBody
    public Map<String,Object> delVip(@PathVariable int id){
        Map<String, Object> map = new HashMap<>();
        System.out.println(id);
        setMatonRemote.delVip(id);
//        System.out.println("删除成功！》》》");
//        map.put("result","删除成功！");
        return map;
    }

    @GetMapping("/addVip")
    @ResponseBody
    public Map<String,Object> addVip(Vip vip){
        HashMap<String, Object> map = new HashMap<>();
        int default_level = vip.getDefault_level();
        if(default_level == 1){
            //修改全字段
            setMatonRemote.updRankAll();
            setMatonRemote.addVip(vip);
        }else {

        setMatonRemote.addVip(vip);
        }
        System.out.println("添加成功!");
        return map;
    }

    @GetMapping("/updInfo")
    @ResponseBody
    public Map<String,Object> updInfo(Vip vip){
        HashMap<String, Object> map = new HashMap<>();
        int default_level = vip.getDefault_level();
        int id = vip.getId();


        if(default_level == 1){
            setMatonRemote.updRankAll();
            setMatonRemote.updInfo(vip);
        }else{
            setMatonRemote.updInfo(vip);
        }
        System.out.println("修改成功!>>>id:"+id);
        return map;
    }


    //发送邮件
    @GetMapping("/email")
    @ResponseBody
    public Map<String,Object> getEmail(int id,String email){
        HashMap<String, Object> map = new HashMap<>();
        System.out.println("进入User发送邮件！"+id+">>>>"+email);
       setMatonRemote.getEmail(id, email);
        map.put("result", "成功！");
        map.put("data", "");
        map.put("isSuccess", true);
        return map;
    }
    //发送短信
    @GetMapping("/sms")
    @ResponseBody
    public Map<String,Object> getSMS(int id,String num){
        HashMap<String, Object> map = new HashMap<>();
        System.out.println("进入User发送短信！"+id+">>>>"+num);
        setMatonRemote.getSMS(id, num);
        map.put("result", "成功！");
        map.put("data", "");
        map.put("isSuccess", true);
        return map;
    }

    //一起发送
    @GetMapping("/sendAll")
    @ResponseBody
    public Map<String,Object> getAll(int id,String email,String num){
        HashMap<String, Object> map = new HashMap<>();
        System.out.println("进入User发送邮件和短信！"+id+">>>>"+num+">>>>>"+email);
        setMatonRemote.getEmail(id, email);
        setMatonRemote.getSMS(id, num);
        map.put("result", "成功！");
        map.put("data", "");
        map.put("isSuccess", true);
        return map;
    }

}
